//
//  ZHGenderSelectCell.m
//  ZHFormSDK
//
//  Created by YeQing on 2020/10/21.
//

#import "ZHGenderSelectCell.h"
#import "ZHFormSDKPublic.h"
#import "XLForm.h"

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wstrict-prototypes"
#import <Masonry/Masonry.h>
#pragma clang diagnostic pop

#define ZHGenderSelectCell_Boy 0
#define ZHGenderSelectCell_Girl 1

@interface ZHGenderSelectCell()
@property (nonatomic, strong) UILabel *titleLab;
@property (nonatomic, strong) UIButton *boyBtn;
@property (nonatomic, strong) UIButton *girlBtn;
@end

@implementation ZHGenderSelectCell

//MARK: - life cycle
+ (void)load {
    [XLFormViewController.cellClassesForRowDescriptorTypes setObject:[ZHGenderSelectCell class] forKey:ZHFormRowDescriptorTypeGenderSelector];
}

//MARK: - property
- (UILabel *)titleLab {
    if (!_titleLab) {
        UILabel *view = [[UILabel alloc] init];
        view.font = BCComConfig.config.bcFont(16);
        view.textColor = BCComConfig.config.blackColor;
        _titleLab = view;
        [self.contentView addSubview:view];
    }
    return _titleLab;
}
-(UIButton *)boyBtn {
    if (!_boyBtn) {
        UIButton *view = [UIButton buttonWithType:UIButtonTypeCustom];
        view.titleLabel.font = BCComConfig.config.bcFont(14);
        [view setTitle:@"男孩" forState:UIControlStateNormal];
        [view setTitleColor:BCComConfig.shared.littleBlackColor forState:UIControlStateNormal];
        [view setBackgroundImage:[UIImage bc_imageWithColor:[UIColor bc_colorWithRGB:0xF0F0F0]] forState:UIControlStateNormal];
        [view setTitle:@"男孩" forState:UIControlStateSelected];
        [view setTitleColor:UIColor.whiteColor forState:UIControlStateSelected];
        [view setBackgroundImage:[UIImage bc_imageWithColor:BCComConfig.shared.btnPressBgColor] forState:UIControlStateSelected];
        view.layer.cornerRadius = 14;
        view.layer.masksToBounds = true;
        [view addTarget:self action:@selector(boyBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        _boyBtn = view;
        [self.contentView addSubview:view];
    }
    return _boyBtn;
}
-(UIButton *)girlBtn {
    if (!_girlBtn) {
        UIButton *view = [UIButton buttonWithType:UIButtonTypeCustom];
        view.titleLabel.font = BCComConfig.config.bcFont(14);
        [view setTitle:@"女孩" forState:UIControlStateNormal];
        [view setTitleColor:BCComConfig.shared.littleBlackColor forState:UIControlStateNormal];
        [view setBackgroundImage:[UIImage bc_imageWithColor:[UIColor bc_colorWithRGB:0xF0F0F0]] forState:UIControlStateNormal];
        [view setTitle:@"女孩" forState:UIControlStateSelected];
        [view setTitleColor:UIColor.whiteColor forState:UIControlStateSelected];
        [view setBackgroundImage:[UIImage bc_imageWithColor:BCComConfig.shared.btnPressBgColor] forState:UIControlStateSelected];
        view.layer.cornerRadius = 14;
        view.layer.masksToBounds = true;
        [view addTarget:self action:@selector(girlBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        _girlBtn = view;
        [self.contentView addSubview:view];
    }
    return _girlBtn;
}

//MARK: - Event
- (void)boyBtnClick:(UIButton *)sender {
    if (sender.selected) { return; }
    sender.selected = !sender.selected;
    self.girlBtn.selected = !sender.selected;
    self.rowDescriptor.value = [NSNumber numberWithInteger:ZHGenderSelectCell_Boy];
    if (self.rowDescriptor.reloadCallback) {
        self.rowDescriptor.reloadCallback(self.rowDescriptor);
    }
}
- (void)girlBtnClick:(UIButton *)sender {
    if (sender.selected) { return; }
    sender.selected = !sender.selected;
    self.boyBtn.selected = !sender.selected;
    self.rowDescriptor.value = [NSNumber numberWithInteger:ZHGenderSelectCell_Girl];
    if (self.rowDescriptor.reloadCallback) {
        self.rowDescriptor.reloadCallback(self.rowDescriptor);
    }
}

//MARK: - XLFormDescriptorCell
-(void)configure {
    [super configure];
    [self setSelectionStyle:UITableViewCellSelectionStyleNone];
    [self.contentView addSubview:self.asteriskLab];
    [self.asteriskLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.centerY.mas_equalTo(self.contentView);
    }];
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.centerY.mas_equalTo(self.contentView);
        make.height.mas_equalTo(self.contentView);
    }];
    [self.girlBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-1);
        make.centerY.mas_equalTo(self.contentView);
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(28);
    }];
    [self.boyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.girlBtn.mas_left).offset(-15);
        make.centerY.mas_equalTo(self.girlBtn);
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(28);
    }];
    
}
-(void)update {
    [super update];
    self.accessoryType = UITableViewCellAccessoryNone;
    self.asteriskLab.hidden = !self.rowDescriptor.required;
    //update title
    if (self.rowDescriptor.titleFont) {
        self.titleLab.font = self.rowDescriptor.titleFont;
    }
    if (self.rowDescriptor.titleColor) {
        self.titleLab.textColor = self.rowDescriptor.titleColor;
    }
    if (self.rowDescriptor.titleAlignment) {
        self.titleLab.textAlignment = self.rowDescriptor.titleAlignment;
    }
    if (self.rowDescriptor.titleWidth>0) {
        [self.titleLab mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(self.rowDescriptor.titleWidth);
        }];
    }
    if (self.rowDescriptor.titleLeft>0) {
        [self.titleLab mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.rowDescriptor.titleLeft);
        }];
        [self.asteriskLab mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(_titleLab.mas_left).offset(-1);
            make.centerY.mas_equalTo(self.contentView);
        }];
    }
    self.titleLab.text = self.rowDescriptor.title;
    // update gender
    if (self.rowDescriptor.textFont) {
        self.boyBtn.titleLabel.font = self.rowDescriptor.textFont;
        self.girlBtn.titleLabel.font = self.rowDescriptor.textFont;
    }
    if (self.rowDescriptor.textAlignment != nil && [self.rowDescriptor.textAlignment integerValue] == NSTextAlignmentLeft) {
        //靠左
        NSInteger left = (self.rowDescriptor.textLeft >0) ? self.rowDescriptor.textLeft : 95 ;
        [self.boyBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(left);
            make.centerY.mas_equalTo(self.contentView);
            make.width.mas_equalTo(80);
            make.height.mas_equalTo(28);
        }];
        [self.girlBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.boyBtn.mas_right).offset(15);
            make.centerY.mas_equalTo(self.boyBtn);
            make.width.mas_equalTo(80);
            make.height.mas_equalTo(28);
        }];
    }
    else {
        //靠右
        [self.girlBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(-1);
            make.centerY.mas_equalTo(self.contentView);
            make.width.mas_equalTo(80);
            make.height.mas_equalTo(28);
        }];
        [self.boyBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(self.girlBtn.mas_left).offset(-15);
            make.centerY.mas_equalTo(self.girlBtn);
            make.width.mas_equalTo(80);
            make.height.mas_equalTo(28);
        }];
    }
    [self.boyBtn setEnabled:!self.rowDescriptor.isDisabled];
    [self.girlBtn setEnabled:!self.rowDescriptor.isDisabled];
    //男孩
    NSString *boy_title = (NSString *)self.rowDescriptor.extData[@"boy_title"];
    if (boy_title) {
        [self.boyBtn setTitle:boy_title forState:UIControlStateNormal];
    }
    UIImage *boy_icon = (UIImage *)self.rowDescriptor.extData[@"boy_icon"];
    if (boy_icon) {
        [self.boyBtn setImage:boy_icon forState:UIControlStateNormal];
        self.boyBtn.imageEdgeInsets = UIEdgeInsetsMake(0, -2, 0, 2);
    }
    UIImage *boy_icon_sel = (UIImage *)self.rowDescriptor.extData[@"boy_icon_sel"];
    if (boy_icon_sel) {
        [self.boyBtn setImage:boy_icon_sel forState:UIControlStateSelected];
    }
    //女孩
    NSString *girl_title = (NSString *)self.rowDescriptor.extData[@"girl_title"];
    if (girl_title) {
        [self.girlBtn setTitle:girl_title forState:UIControlStateNormal];
    }
    UIImage *girl_icon = (UIImage *)self.rowDescriptor.extData[@"girl_icon"];
    if (girl_icon) {
        [self.girlBtn setImage:girl_icon forState:UIControlStateNormal];
        self.girlBtn.imageEdgeInsets = UIEdgeInsetsMake(0, -2, 0, 2);
    }
    UIImage *girl_icon_sel = (UIImage *)self.rowDescriptor.extData[@"girl_icon_sel"];
    if (girl_icon_sel) {
        [self.girlBtn setImage:girl_icon_sel forState:UIControlStateSelected];
    }
    if (self.rowDescriptor.value != nil) {
        NSInteger selectedGender = [self.rowDescriptor.value integerValue];
        self.boyBtn.selected = (selectedGender == ZHGenderSelectCell_Boy);
        self.girlBtn.selected = (selectedGender == ZHGenderSelectCell_Girl);
    }
}


#pragma mark - override
- (BOOL)canBecomeFirstResponder {
    return !self.rowDescriptor.isDisabled;
}
@end
